//+------------------------------------------------------------------+
//|                                                    GUIModule.mqh |
//|                                      GUI界面模块 - 负责界面管理   |
//|                                                                  |
//+------------------------------------------------------------------+
#property strict

//+------------------------------------------------------------------+
//| 界面控件定义                                                      |
//+------------------------------------------------------------------+
#define PANEL_WIDTH     300
#define PANEL_HEIGHT    500
#define BUTTON_WIDTH    120
#define BUTTON_HEIGHT   35
#define MARGIN          10
#define PADDING_TOP     15      // 上内边距
#define PADDING_BOTTOM  15      // 下内边距
#define PADDING_LEFT    15      // 左内边距
#define PADDING_RIGHT   15      // 右内边距

// 布局间距定义 - 类似Web标准布局
#define TITLE_HEIGHT    20      // 标题区域高度
#define SECTION_SPACING 25      // 区块间距
#define LINE_HEIGHT     25      // 行高
#define LABEL_SPACING   10      // 标签间距

// 控件名称
#define PANEL_NAME      "TradingPanel"
#define SYMBOL_LABEL    "SymbolLabel"
#define BID_LABEL       "BidLabel"
#define ASK_LABEL       "AskLabel"
#define SPREAD_LABEL    "SpreadLabel"
#define ENTRY_EDIT      "EntryEdit"
#define SL_EDIT         "SLEdit"
#define RISK_EDIT       "RiskEdit"
#define LOTS_LABEL      "LotsLabel"

// 按钮名称
#define BTN_BUY         "BtnBuy"
#define BTN_SELL        "BtnSell"
#define BTN_CLOSE_BUY   "BtnCloseBuy"
#define BTN_CLOSE_SELL  "BtnCloseSell"
#define BTN_CLOSE_ALL   "BtnCloseAll"
#define BTN_BREAKEVEN   "BtnBreakeven"

//+------------------------------------------------------------------+
//| GUI界面模块类                                                     |
//+------------------------------------------------------------------+
class CGUIModule
{
private:
   string            m_symbol;          // 交易品种
   double            m_riskPercent;     // 风险百分比
   bool              m_isCreated;       // 界面是否已创建
   
   // 界面位置
   int               m_panelX;
   int               m_panelY;
   
public:
   // 构造函数
   CGUIModule(string symbol, double riskPercent);
   
   // 析构函数
   ~CGUIModule();
   
   // 创建界面
   bool              CreateInterface();
   
   // 删除界面
   void              DeleteInterface();
   
   // 更新价格显示
   void              UpdatePriceDisplay(double bid, double ask, double spread);
   
   // 更新手数显示
   void              UpdateLotSizeDisplay(double lotSize);
   
   // 获取输入值
   double            GetEntryPrice();
   double            GetStopLoss();
   double            GetRiskPercent();
   double            GetLotSize();
   
   // 设置控件值
   void              SetEntryPrice(double price);
   void              SetStopLoss(double price);
   void              SetRiskPercent(double percent);
   
   // 检查按钮点击
   string            CheckButtonClick(string clickedObject);
   
   // 重置按钮状态
   void              ResetButtonState(string buttonName);
   
private:
   // 创建控件辅助函数
   bool              CreateLabel(string name, string text, int x, int y, int font_size, color clr, string font = "Arial");
   bool              CreateButton(string name, string text, int x, int y, int width, int height, color clr);
   bool              CreateEdit(string name, string text, int x, int y, int width, int height);
};

//+------------------------------------------------------------------+
//| 构造函数                                                          |
//+------------------------------------------------------------------+
CGUIModule::CGUIModule(string symbol, double riskPercent)
{
   m_symbol = symbol;
   m_riskPercent = riskPercent;
   m_isCreated = false;
   m_panelX = 20;
   m_panelY = 30;
}

//+------------------------------------------------------------------+
//| 析构函数                                                          |
//+------------------------------------------------------------------+
CGUIModule::~CGUIModule()
{
   DeleteInterface();
}

//+------------------------------------------------------------------+
//| 创建界面                                                          |
//+------------------------------------------------------------------+
bool CGUIModule::CreateInterface()
{
   if(m_isCreated) return true;
   
   int x = m_panelX, y = m_panelY;
   
   // 创建主面板背景
   if(!ObjectCreate(0, PANEL_NAME, OBJ_RECTANGLE_LABEL, 0, 0, 0))
   {
      Print("创建主面板失败");
      return false;
   }
   ObjectSetInteger(0, PANEL_NAME, OBJPROP_XDISTANCE, x);
   ObjectSetInteger(0, PANEL_NAME, OBJPROP_YDISTANCE, y);
   ObjectSetInteger(0, PANEL_NAME, OBJPROP_XSIZE, PANEL_WIDTH);
   ObjectSetInteger(0, PANEL_NAME, OBJPROP_YSIZE, PANEL_HEIGHT);
   ObjectSetInteger(0, PANEL_NAME, OBJPROP_BGCOLOR, clrWhite);
   ObjectSetInteger(0, PANEL_NAME, OBJPROP_BORDER_TYPE, BORDER_RAISED);
   ObjectSetInteger(0, PANEL_NAME, OBJPROP_CORNER, CORNER_LEFT_UPPER);
   ObjectSetInteger(0, PANEL_NAME, OBJPROP_COLOR, clrGold);
   ObjectSetInteger(0, PANEL_NAME, OBJPROP_STYLE, STYLE_SOLID);
   ObjectSetInteger(0, PANEL_NAME, OBJPROP_WIDTH, 3);
   ObjectSetInteger(0, PANEL_NAME, OBJPROP_BACK, false);
   ObjectSetInteger(0, PANEL_NAME, OBJPROP_SELECTABLE, false);
   ObjectSetInteger(0, PANEL_NAME, OBJPROP_SELECTED, false);
   ObjectSetInteger(0, PANEL_NAME, OBJPROP_HIDDEN, true);
   
   // 计算布局位置 - Web标准布局方式
   int content_x = x + PADDING_LEFT;
   int content_y = y + PADDING_TOP;
   int current_y = content_y;
   
   // 创建标题区域
   CreateLabel("Title", "智能交易小助手", content_x, current_y, 14, clrBlack, "Arial Bold");
   current_y += TITLE_HEIGHT + SECTION_SPACING;
   
   // 创建品种显示区域
   CreateLabel(SYMBOL_LABEL, m_symbol, content_x, current_y, 18, clrBlue, "Arial Bold");
   current_y += LINE_HEIGHT + SECTION_SPACING;
   
   // 创建价格显示区域
   CreateLabel("BidText", "买价:", content_x, current_y, 10, clrBlack);
   CreateLabel(BID_LABEL, "0.00000", content_x + 50, current_y, 12, clrRed, "Arial Bold");
   
   CreateLabel("AskText", "卖价:", content_x + 140, current_y, 10, clrBlack);
   CreateLabel(ASK_LABEL, "0.00000", content_x + 190, current_y, 12, clrBlue, "Arial Bold");
   current_y += LINE_HEIGHT;
   
   CreateLabel("SpreadText", "点差:", content_x, current_y, 10, clrBlack);
   CreateLabel(SPREAD_LABEL, "0", content_x + 50, current_y, 12, clrGreen, "Arial Bold");
   current_y += LINE_HEIGHT + SECTION_SPACING;
   
   // 创建输入区域
   CreateLabel("EntryText", "入场价位:", content_x, current_y, 10, clrBlack);
   CreateEdit(ENTRY_EDIT, "", content_x + 70, current_y - 5, 100, 25);
   current_y += LINE_HEIGHT + LABEL_SPACING;
   
   CreateLabel("SLText", "止损价位:", content_x, current_y, 10, clrBlack);
   CreateEdit(SL_EDIT, "", content_x + 70, current_y - 5, 100, 25);
   current_y += LINE_HEIGHT + LABEL_SPACING;
   
   CreateLabel("RiskText", "回撤比例:", content_x, current_y, 10, clrBlack);
   CreateEdit(RISK_EDIT, DoubleToString(m_riskPercent, 2) + "%", content_x + 70, current_y - 5, 60, 25);
   
   CreateLabel("LotsText", "实际仓位:", content_x + 140, current_y, 10, clrBlack);
   CreateLabel(LOTS_LABEL, "0.01", content_x + 210, current_y, 10, clrPurple, "Arial Bold");
   current_y += LINE_HEIGHT + SECTION_SPACING;
   
   // 创建交易按钮区域
   CreateButton(BTN_BUY, "一键下单", content_x, current_y, BUTTON_WIDTH * 2 + 10, BUTTON_HEIGHT, clrGreen);
   current_y += BUTTON_HEIGHT + LABEL_SPACING;
   
   CreateButton(BTN_SELL, "一键做空", content_x, current_y, BUTTON_WIDTH * 2 + 10, BUTTON_HEIGHT, clrRed);
   current_y += BUTTON_HEIGHT + LABEL_SPACING;
   
   CreateButton(BTN_CLOSE_BUY, "平仓多单", content_x, current_y, BUTTON_WIDTH, BUTTON_HEIGHT, clrOrange);
   CreateButton(BTN_CLOSE_SELL, "平仓空单", content_x + BUTTON_WIDTH + 10, current_y, BUTTON_WIDTH, BUTTON_HEIGHT, clrOrange);
   current_y += BUTTON_HEIGHT + LABEL_SPACING;
   
   CreateButton(BTN_BREAKEVEN, "删除挂单", content_x, current_y, BUTTON_WIDTH * 2 + 10, BUTTON_HEIGHT, clrGray);
   current_y += BUTTON_HEIGHT + LABEL_SPACING;
   
   CreateButton(BTN_CLOSE_ALL, "一键清仓", content_x, current_y, BUTTON_WIDTH * 2 + 10, BUTTON_HEIGHT, clrMaroon);
   
   m_isCreated = true;
   ChartRedraw();
   return true;
}

//+------------------------------------------------------------------+
//| 删除界面                                                          |
//+------------------------------------------------------------------+
void CGUIModule::DeleteInterface()
{
   if(!m_isCreated) return;
   
   ObjectDelete(0, PANEL_NAME);
   ObjectDelete(0, "Title");
   ObjectDelete(0, SYMBOL_LABEL);
   ObjectDelete(0, "BidText");
   ObjectDelete(0, BID_LABEL);
   ObjectDelete(0, "AskText");
   ObjectDelete(0, ASK_LABEL);
   ObjectDelete(0, "SpreadText");
   ObjectDelete(0, SPREAD_LABEL);
   ObjectDelete(0, "EntryText");
   ObjectDelete(0, ENTRY_EDIT);
   ObjectDelete(0, "SLText");
   ObjectDelete(0, SL_EDIT);
   ObjectDelete(0, "RiskText");
   ObjectDelete(0, RISK_EDIT);
   ObjectDelete(0, "LotsText");
   ObjectDelete(0, LOTS_LABEL);
   ObjectDelete(0, BTN_BUY);
   ObjectDelete(0, BTN_SELL);
   ObjectDelete(0, BTN_CLOSE_BUY);
   ObjectDelete(0, BTN_CLOSE_SELL);
   ObjectDelete(0, BTN_BREAKEVEN);
   ObjectDelete(0, BTN_CLOSE_ALL);
   
   m_isCreated = false;
   ChartRedraw();
}

//+------------------------------------------------------------------+
//| 更新价格显示                                                      |
//+------------------------------------------------------------------+
void CGUIModule::UpdatePriceDisplay(double bid, double ask, double spread)
{
   if(!m_isCreated) return;
   
   ObjectSetString(0, BID_LABEL, OBJPROP_TEXT, DoubleToString(bid, Digits));
   ObjectSetString(0, ASK_LABEL, OBJPROP_TEXT, DoubleToString(ask, Digits));
   ObjectSetString(0, SPREAD_LABEL, OBJPROP_TEXT, IntegerToString((int)spread));
   
   ChartRedraw();
}

//+------------------------------------------------------------------+
//| 更新手数显示                                                      |
//+------------------------------------------------------------------+
void CGUIModule::UpdateLotSizeDisplay(double lotSize)
{
   if(!m_isCreated) return;
   
   ObjectSetString(0, LOTS_LABEL, OBJPROP_TEXT, DoubleToString(lotSize, 2));
   ChartRedraw();
}

//+------------------------------------------------------------------+
//| 获取入场价格                                                      |
//+------------------------------------------------------------------+
double CGUIModule::GetEntryPrice()
{
   if(!m_isCreated) return 0;
   return StringToDouble(ObjectGetString(0, ENTRY_EDIT, OBJPROP_TEXT));
}

//+------------------------------------------------------------------+
//| 获取止损价格                                                      |
//+------------------------------------------------------------------+
double CGUIModule::GetStopLoss()
{
   if(!m_isCreated) return 0;
   return StringToDouble(ObjectGetString(0, SL_EDIT, OBJPROP_TEXT));
}

//+------------------------------------------------------------------+
//| 获取风险百分比                                                    |
//+------------------------------------------------------------------+
double CGUIModule::GetRiskPercent()
{
   if(!m_isCreated) return m_riskPercent;
   string riskText = ObjectGetString(0, RISK_EDIT, OBJPROP_TEXT);
   StringReplace(riskText, "%", "");
   return StringToDouble(riskText);
}

//+------------------------------------------------------------------+
//| 获取手数                                                          |
//+------------------------------------------------------------------+
double CGUIModule::GetLotSize()
{
   if(!m_isCreated) return 0.01;
   return StringToDouble(ObjectGetString(0, LOTS_LABEL, OBJPROP_TEXT));
}

//+------------------------------------------------------------------+
//| 检查按钮点击                                                      |
//+------------------------------------------------------------------+
string CGUIModule::CheckButtonClick(string clickedObject)
{
   if(clickedObject == BTN_BUY) return "BUY";
   if(clickedObject == BTN_SELL) return "SELL";
   if(clickedObject == BTN_CLOSE_BUY) return "CLOSE_BUY";
   if(clickedObject == BTN_CLOSE_SELL) return "CLOSE_SELL";
   if(clickedObject == BTN_CLOSE_ALL) return "CLOSE_ALL";
   if(clickedObject == BTN_BREAKEVEN) return "DELETE_PENDING";
   
   return "";
}

//+------------------------------------------------------------------+
//| 重置按钮状态                                                      |
//+------------------------------------------------------------------+
void CGUIModule::ResetButtonState(string buttonName)
{
   if(!m_isCreated) return;
   ObjectSetInteger(0, buttonName, OBJPROP_STATE, false);
}

//+------------------------------------------------------------------+
//| 创建标签控件                                                      |
//+------------------------------------------------------------------+
bool CGUIModule::CreateLabel(string name, string text, int x, int y, int font_size, color clr, string font = "Arial")
{
   if(!ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0))
      return false;
      
   ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x);
   ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y);
   ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_LEFT_UPPER);
   ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_LEFT_UPPER);
   ObjectSetString(0, name, OBJPROP_TEXT, text);
   ObjectSetString(0, name, OBJPROP_FONT, font);
   ObjectSetInteger(0, name, OBJPROP_FONTSIZE, font_size);
   ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
   ObjectSetInteger(0, name, OBJPROP_BACK, false);
   ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
   ObjectSetInteger(0, name, OBJPROP_SELECTED, false);
   ObjectSetInteger(0, name, OBJPROP_HIDDEN, true);
   
   return true;
}

//+------------------------------------------------------------------+
//| 创建按钮控件                                                      |
//+------------------------------------------------------------------+
bool CGUIModule::CreateButton(string name, string text, int x, int y, int width, int height, color clr)
{
   if(!ObjectCreate(0, name, OBJ_BUTTON, 0, 0, 0))
      return false;
      
   ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x);
   ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y);
   ObjectSetInteger(0, name, OBJPROP_XSIZE, width);
   ObjectSetInteger(0, name, OBJPROP_YSIZE, height);
   ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_LEFT_UPPER);
   ObjectSetString(0, name, OBJPROP_TEXT, text);
   ObjectSetString(0, name, OBJPROP_FONT, "Arial Bold");
   ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 10);
   ObjectSetInteger(0, name, OBJPROP_COLOR, clrWhite);
   ObjectSetInteger(0, name, OBJPROP_BGCOLOR, clr);
   ObjectSetInteger(0, name, OBJPROP_BORDER_COLOR, clrBlack);
   ObjectSetInteger(0, name, OBJPROP_BACK, false);
   ObjectSetInteger(0, name, OBJPROP_STATE, false);
   ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
   ObjectSetInteger(0, name, OBJPROP_SELECTED, false);
   ObjectSetInteger(0, name, OBJPROP_HIDDEN, true);
   
   return true;
}

//+------------------------------------------------------------------+
//| 创建编辑框控件                                                    |
//+------------------------------------------------------------------+
bool CGUIModule::CreateEdit(string name, string text, int x, int y, int width, int height)
{
   if(!ObjectCreate(0, name, OBJ_EDIT, 0, 0, 0))
      return false;
      
   ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x);
   ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y);
   ObjectSetInteger(0, name, OBJPROP_XSIZE, width);
   ObjectSetInteger(0, name, OBJPROP_YSIZE, height);
   ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_LEFT_UPPER);
   ObjectSetString(0, name, OBJPROP_TEXT, text);
   ObjectSetString(0, name, OBJPROP_FONT, "Arial");
   ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 9);
   ObjectSetInteger(0, name, OBJPROP_COLOR, clrBlack);
   ObjectSetInteger(0, name, OBJPROP_BGCOLOR, clrWhite);
   ObjectSetInteger(0, name, OBJPROP_BORDER_COLOR, clrGray);
   ObjectSetInteger(0, name, OBJPROP_BACK, false);
   ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
   ObjectSetInteger(0, name, OBJPROP_SELECTED, false);
   ObjectSetInteger(0, name, OBJPROP_HIDDEN, true);
   ObjectSetInteger(0, name, OBJPROP_READONLY, false);
   
   return true;
}